﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace YmtAuth.Domain.DomainService
{
    public class IpMatchSpec
    {
        private static readonly object o = new object();

        private static Dictionary<string, Tuple<int, bool>> ipBulackSpecDic = new Dictionary<string, Tuple<int, bool>>();
        private static Dictionary<string, string> ipAllMatchDic = new Dictionary<string, string>();


        public static Dictionary<string, string> IpAllMatchDictionary
        {
            get { return ipAllMatchDic; }
        }

        public static Dictionary<string, Tuple<int, bool>> IpBulackSpecDictionary
        {
            get
            {
                lock (o)
                {
                    return ipBulackSpecDic;
                }


            }
        }

        public static void SetIpMatchSpec(string ipExpressionSpec, string ipAllMatchExpression)
        {
            lock (o)
            {
                if ( /*ipBulackSpecDic.Count <= 0 &&*/ !string.IsNullOrEmpty(ipExpressionSpec))
                {
                    var ipArray = ipExpressionSpec.Split(new char[] {';'});
                    ipArray.Each(
                        ip => ipBulackSpecDic[ip] = Tuple.Create(ip.Split(new char[] {'.'}).Length, ip.Contains("-")));
                }
                if ( /*ipAllMatchDic.Count <= 0 &&*/ !string.IsNullOrEmpty(ipAllMatchExpression))
                {
                    ipAllMatchExpression.Split(new char[] {(';')}).Each(i => ipAllMatchDic[i] = i);
                }
            }
        }
    }
}
